package com.mylove.judas.yaya.common.config;

import android.content.Context;
import android.os.Environment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.GlideModule;

import java.io.File;

public class GlideConfiguration implements GlideModule {

    private static final int MEMORY_MAX_SPACE=(int) (Runtime.getRuntime().maxMemory()/8);

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        //设置加载图片的样式,比默认图片质量好,但占用内存会大点
        builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
        builder.setMemoryCache(new LruResourceCache(MEMORY_MAX_SPACE));
//        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, getDiskFileString(context,"BoShiLun"), 20*1024*1024));


//        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, Environment.getExternalStorageDirectory().getAbsolutePath()+"/BoShiLun", 20*1024*1024));
        String img_cache_dir = Environment.getExternalStorageDirectory().getAbsolutePath()+"/YaYa/bitmaps";
        builder.setDiskCache(new DiskLruCacheFactory(img_cache_dir, 20*1024*1024));
    }

    private String getDiskFileString(Context mContext, String str) {
         File dirFile=new File(mContext.getCacheDir().getAbsolutePath().toString()+str);
            File tempFile=new File(dirFile,"bitmaps");
            if (! tempFile.getParentFile().exists()){
                tempFile.getParentFile().mkdirs();
            }
            return tempFile.getAbsolutePath().toString();
    }

    @Override
    public void registerComponents(Context context, Glide glide) {

    }

}